//
//  StatusPictureView.swift
//  SinaWeibo
//
//  Created by Apple on 15/11/14.
//  Copyright © 2015年 Apple. All rights reserved.
//微博图片

import UIKit
import SnapKit
import SDWebImage
private let PictureCellId = "PictureCellId"
private let PictrueCellMargin: CGFloat =  5
class StatusPictureView: UICollectionView {
    var imageURLs: [NSURL]? {
        didSet {
            reloadData()
          print(imageURLs)
            let viewSize = self.calceViewSize()
            self.snp_makeConstraints { (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
        }
       
    }
    //计算配图视图大小
    private func calceViewSize() -> CGSize {
        let colum: CGFloat = 3.0000000000001
        //单张图片宽度
        let maxWidth = screenW - 2 * StatusCellMargin
        let itemWidth = (maxWidth - 2 * PictrueCellMargin) / colum
        //本来就是flowlayout,转一下类型就准确啦啊
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        let imageCount = imageURLs?.count ?? 0
        if imageCount == 0 {
            return CGSizeZero
        }
        if imageCount == 1{
            let key = imageURLs?.last?.absoluteString
            let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key)
            //注意这样设置会直接崩溃,因为有的微博可能是转早的,带有一张图片,所以需要在Status模型里面进行判断 写一个计算型属性
            var imageSize = CGSizeMake(180, 120)
            if image != nil {
                imageSize = image.size
            }
            
            layout.itemSize = imageSize
            
            return imageSize
        }
        //4张图片
        if imageCount == 4{
            let  w = itemWidth * 2 + PictrueCellMargin
            return CGSize(width: w, height: w)
        }
        //多图
        //行
        /*
        1,2,3   -> 1
        4,5,6   -> 2
        7,8,9   -> 3
        */
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = maxWidth
        let h = row * itemWidth + (row - 1) * PictrueCellMargin
        return CGSize(width: w, height: h)
    }
    
    
    //MARK: 3.重写父类的构造方法 调用设置UI
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = PictrueCellMargin
        layout.minimumLineSpacing = PictrueCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        registerClass(PictureCell.self, forCellWithReuseIdentifier: PictureCellId)
        dataSource = self
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:2.设置UI
    private func setupUI(){
        backgroundColor = UIColor.randomColor()

    }
    
    //MARK:1.懒加载控件

}

    //MARK: 数据源方法
extension StatusPictureView: UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureCell
        cell.url =  imageURLs![indexPath.item]
        return cell

    }
    
    
}

//MARK:UICollectionViewCell
class PictureCell:UICollectionViewCell {
    
    var url:  NSURL? {
        didSet{
            imageView
            .sd_setImageWithURL(url, placeholderImage: nil)
        }
    }
    
    
    //MARK: 3.重写父类的构造方法 调用设置UI
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:2.设置UI
    private func setupUI(){
        contentView.addSubview(imageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    //MARK:1.懒加载控件
    private lazy var imageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .ScaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
}